import axios from 'axios';

export const base = 'http://123.206.107.77:8085';

function $_post(url, params) {
    let userToken = sessionStorage.getItem('userToken');
    let header = { 'Authorization': userToken};

    return axios.post(url, params, { headers: header });
}

function  $_get(url, params) {
    let userToken = sessionStorage.getItem('userToken');
    let header = { 'Authorization': userToken};
    if (params != null) {
        url += '?';
        for (let key in params) {
            if (params[key] != null && params[key] != '') {
                url += key + '=' + params[key] + '&';
            }
        }
    }

    if (url.charAt(url.length-1) == "&") {
        url = url.substring(0, url.length - 1);
    }

    return axios.get(url, { headers: header });
}

export const requestLogin = params => { return axios.post(`${base}/user/login`, params).then(res => res.data); };

export const getUserListPage = params => { return $_get(`${base}/user/list`, params); };

export const removeUser = id => { return $_get(`${base}/user/remove/${id}`); };

export const batchRemoveUser = params => { return $_get(`${base}/user/batchremove`, params); };

export const editUser =  (id, params) => { return $_post(`${base}/user/update/${id}`, params); };

export const changPasswordUser =  (id, params) => { return $_post(`${base}/user/password/chang/${id}`, params); };

export const addUser = params => { return $_post(`${base}/user/create`, params); };

export const uploadImage = params => { return $_post(`${base}/upload/add`, params); };

export const getTemplateListPage = params => { return $_get(`${base}/template/list`, params); };

export const editTemplate =  (id, params) => { return $_post(`${base}/template/update/${id}`, params); };

export const addTemplate = params => { return $_post(`${base}/template/create`, params); };

export const removeTemplate  = id => { return $_get(`${base}/template/remove/${id}`); };

export const pushMessage  = params => { return $_post(`${base}/message/push`, params); };

export const messageCount  = params => { return $_get(`${base}/message/count`); };

export const syncUser  = params => { return $_get(`${base}/user/sync`); };

export const getSendLogListPage = params => { return $_get(`${base}/send_log/list`, params); };
